/*
 *                    Copyright (c), NXP Semiconductors
 *
 *                       (C) NXP Semiconductors 2014,2015
 *
 *         All rights are reserved. Reproduction in whole or in part is
 *        prohibited without the written consent of the copyright owner.
 *    NXP reserves the right to make changes without notice at any time.
 *   NXP makes no warranty, expressed, implied or statutory, including but
 *   not limited to any implied warranty of merchantability or fitness for any
 *  particular purpose, or that the use will not infringe any third party patent,
 *   copyright or trademark. NXP must not be liable for any loss or damage
 *                            arising from its use.
 */

/** @ file
 *
 * Contact Interface : Protocol Abstraction Layer
 *
 * Project:  PN7462AU
 *
 * $Date: 2015-12-17 15:55:08 +0530 (Thu, 17 Dec 2015) $
 * $Author: Prasad Hegde (nxp72764) $
 * $Revision: 17033 $ (v04.16.00)
 *
 */

/* *******************************************************************************************************************
 * Includes
 * ****************************************************************************************************************** */
#include "ph_NxpBuild.h"

#ifdef NXPBUILD__PHPAL_CT
#include <ph_Status.h>
#include "phpalCt.h"
#include "ISO7816/i7816.h"

/* ******************************************************************************************************************
 * Global Defines
 * ***************************************************************************************************************** */

/* ******************************************************************************************************************
 * Type Definitions
 * ***************************************************************************************************************** */
/**
 *
 */

/* ******************************************************************************************************************
 * GLOBAL AND STATIC VARIABLES
 * ***************************************************************************************************************** */
/**
 * Final protocol selected
 */
//	uint8_t gphpalCt_ProtSel;
//	uint8_t gphpalCt_BIFSC; /* Global variable to store IFSC value from ATR for PAL.*/
//	uint8_t gphpalCt_BEmvEn;/* Global variable to store EMVCo or NonEMVCo profile for PAL.*/
//	uint8_t gphpalCt_BSadDad;/* NAD for  card to terminal communication.*/
//	uint8_t gphpalCt_BDadSad;/* NAD for terminal to card communication.*/


/* *******************************************************************************************************************
 *   Public Functions
 * *******************************************************************************************************************/

/**
 * @brief This Api is used to initialize the CT IP and is called from the upper layer
 * @param psCtInitParams   Pointer to Structure which contains Ct initialization parameters.
 *         PH_ERR_FAILED - If resetting the CP IP is unsuccessful
 */
phStatus_t phpalCt_Init(void)
{
    phStatus_t eStatus = PH_ERR_SUCCESS;
	i7816_init();
    return eStatus;
}


/**
 *@brief   phpalHwCt_ActivateCard, This Function does a cold reset/activate of the card for ATR reception,
 *         the API performs three iterations for different vcc selection internally.
 *
 *@param   pDataParams - Pointer to the context structure of the PAL layer
 *         dwSlotNumber - Slot number of of which the card is present and activation has to be carried out
 *@return  PH_ERR_SUCCESS - If the cold activation is success and ATR is received
 *
 */
phStatus_t phpalCt_ActivateCard(phpalCt_DataParams_t *pDataParams)
{
    phStatus_t eStatus = PH_ERR_INVALID_PARAMETER;
	i7816_status_t status = i7816_power_on(0);
	if(status != I7816_S_OK){
		status = i7816_power_on(0);
	}

	if((status == I7816_S_OK)){
		i7816_get_atr(0, pDataParams->pbAtrBuffer, &pDataParams->bAtrReceivedLength);
		eStatus = PH_ERR_SUCCESS;
	}
    return eStatus;
}


/**
 *
 * @brief   This Api calls the appropriate APi's of the underlying protocol for a T0 or T1 related transcieve.
 * @param   pbTransmitBuff - Pointer to the transmit buffer
 * @param   dwTransmitSize - Number of the bytes to be transmitted
 * @param   pbReceiveBuff - Pointer to the receive buffer
 * @param   dwReceiveSize - Pointer to the receive buffer size
 * @return  PH_ERR_INVALID_PARAMETER - If the parameters passed has any errors
 *          PH_ERR_CT_MAIN_CARD_ABSENT - If the card is absent in the main slot
 *          PH_ERR_SUCCESS - If the transaction with the card is successful
 */
phStatus_t phpalCt_Transceive(uint8_t* pbTransmitBuff, uint32_t dwTransmitSize,
                              uint8_t* pbReceiveBuff,  uint16_t* pwReceiveSize)
{
    phStatus_t eStatus = PH_ERR_IO_TIMEOUT;
	i7816_status_t status = i7816_exchange_apdu(0, pbTransmitBuff, dwTransmitSize, pbReceiveBuff, pwReceiveSize);
	if(status == I7816_S_OK){
		eStatus = PH_ERR_SUCCESS;
	}
	else{
		i7816_power_off(0);
	}

    return eStatus;
}


/**
 *@brief   This function is used to deinitialize the CT IP and disable the NVIC for contact interface
 *@param   void
 *@return  #PH_ERR_SUCCESS If Card Deactivate is done successfully
 *@retval #PH_ERR_CT_MAIN_CARD_PRESENT If the card is absent in the slot
 */
phStatus_t phpalCt_DeInit(void)
{
    phStatus_t eStatus = PH_ERR_SUCCESS;
	i7816_power_off(0);
    return eStatus;
}



#endif /* NXPBUILD__PHPAL_CT */

